package com.mall.service.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsRoleResourceRelation;
import com.mall.service.authority.mapper.UmsRoleResourceRelationMapper;
import com.mall.service.authority.service.UmsRoleResourceRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UmsRoleResourceRelationServiceImpl extends ServiceImpl<UmsRoleResourceRelationMapper, UmsRoleResourceRelation> implements UmsRoleResourceRelationService {

    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    @Override
    public void allocResource(Integer roleId, List<Integer> resourceIds) {
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId,roleId);
        umsRoleResourceRelationMapper.delete(queryWrapper);

        for (Integer resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(Long.valueOf(roleId));
            relation.setResourceId(Long.valueOf(resourceId));
            this.save(relation);
        }

    }
}
